var webpack = require('webpack');

var glob = require('glob'); //读取文件和文件夹的模块
var path = require('path');
var FontPlugins = require('font-plugins'); //自动把svg变异成font-face字体
var Spritesify = require('spritesify'); //自动编译sprite图片，写入common.css
var CopyWebpackPlugin = require('copy-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin'); //提取css，生成单独的文件，在页面中link引入
var postcss4lp = require('postcss-lp'); //postCSS依赖
// var BomPlugin = require('webpack-utf8-bom'); //将文件转成utf-8 bom格式，解决中文乱码的问题

// var CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin'); //提取公共的js

var CleanWebpackPlugin = require('clean-webpack-plugin'); //删除文件,自动清理build

// the path(s) that should be cleaned
var pathsToClean = [
  'build'
];

// the clean options to use
let cleanOptions = {
  // root: __dirname, //一个根的绝对路径
  exclude: [], // 排除的文件
  verbose: true,
  dry: false
}

var entrys = glob.sync('./src/js/**/*.js').reduce(function (obj, v) {
  if( /\.js$/.test(v) && !/\/lib\/.+\.js$/.test(v) ){
    var key = v.replace(/\.\/src\/js\/(.+)\.js$/, '$1');
    obj[key] = v;
  }
  return obj;
}, {});
console.log(entrys);

var localPath = 'http://localhost:8888/build/';

module.exports = {
  entry: entrys,
  output: {
    path: './build',
    filename: '/js/[name].js'
  },
  module: {
    loaders: [
      { test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader') },
      {
        test: /\.(jsx?|tpl)$/,
        exclude: /node_modules/,
        loader: 'babel',
        query:
        {
          presets:['es2015','stage-3']
        }
      },
      { 
        test:/\.(png|jpg|gif)(\?.*)?$/, 
        loader: 'url?limit=8192&name=images/[name].[ext]&publicPath=' + localPath,
        exclude: /icons/
      },
      { 
        test:/\.(woff|svg|eot|ttf)(\?.*)?$/, 
        loader: 'file?name=fonts/[name].[ext]&publicPath=' + localPath
      } 
    ],
    preLoaders: [{
      test: /\.tpl?$/,
      loader: 'nodetpl'
    },
    {
      test:   /\.css$/,
      loader: 'postcss-loader'
    },
    {
      test: /common\.css$/,
      loader: 'spritesify',
      query: {
        spritesPath: 'src/images/icons/spritesify',
        distPath: 'build/images/icons',
        //urlfix必须得填写绝对路径
        urlFix: localPath + 'images/icons/'
      }
    }]
  },
  postcss: function() {
    return [
      postcss4lp({
        autoprefixer: { browsers: ['> 1%', 'IE 7'] }
      })
    ];
  },
  resolve:{
      extensions:['','.js','.json']
  },
  // devtool: 'source-map',
  // devServer: {
  //   contentBase: "./public",//本地服务器所加载的页面所在的目录
  //   colors: true,//终端中输出结果为彩色
  //   historyApiFallback: true,//不跳转
  //   inline: true//实时刷新
  // }, 
  plugins: [
  
    // new CopyWebpackPlugin([{
    //     from: 'src/images',
    //     to: 'images'
    // }]),
    new FontPlugins(),
    new CleanWebpackPlugin(pathsToClean, cleanOptions),
    new webpack.NoErrorsPlugin(),
    // new CommonsChunkPlugin('/js/common.js', ['/js/common', '/js/public']),
    new ExtractTextPlugin('/css/[name].css')
    // new CommonsChunkPlugin("commons.js", ["p1", "p2", "admin-commons.js"])
  ]
};


/*
  "devDependencies": {
    "jshint":"^2.9.1",
    "jshint-loader":"^0.8.3",
    "babel-core": "^6.18.2",
    "babel-loader": "^6.2.7",
    "babel-preset-es2015": "^6.18.0",
    "babel-preset-react": "^6.16.0",
    "babel-preset-stage-0": "^6.16.0",
    "copy-webpack-plugin": "4.0.1",
    "postcss-loader": "^0.9.1",
    "css-loader": "^0.28.1",
    "extract-text-webpack-plugin": "^1.0.1",
    "glob": "^7.0.0",
    "html-webpack-plugin": "^2.24.1",
    "style-loader": "^0.13.1",
    "transfer-webpack-plugin": "^0.1.4",
    "webpack": "^1.13.3",
    "webpack-dev-server": "^1.16.2",
    "concurrently": "^3.4.0"
  }*/